import { DiagramTableEditor } from "@/mdesign-lib/mdesign-app.umd.js";
import { Action } from "@/mdesign-lib/mdesign-app.umd.js";
import { ActionKey } from "@/mdesign-lib/mdesign-app.umd.js";
import { archService } from "@/mdesign-lib/mdesign-app.umd.js";
import { PropertyDataType } from "@/mdesign-lib/mdesign-app.umd.js";
import { modelTreeSelectWindow } from "@/mdesign-lib/mdesign-app.umd.js";
import { modelService } from "@/mdesign-lib/mdesign-app.umd.js";

export class CreateActualMeasureAction extends Action {
  desc = "表格新增实际度量";
  keyName = "createActualMeasure" as ActionKey;

  async onTableToolbarClick(tableEditor: DiagramTableEditor) {
    if (tableEditor.disabled) {
      return;
    }
    const diagramId = tableEditor.tab.modelId;

    const row = {
      modelId: diagramId,
      key: "",
      dataType: PropertyDataType.INSTANCE,
      moreBtnCondition: "UAF::Parameters::PropertySet"
    };
    modelTreeSelectWindow
      .popup({
        loadMethod: "getPropertyTreeList",
        showNodeLabel: true,
        loadParams: JSON.stringify({
          row,
          projectId: app.activeProject!.config.id
        }),
        attrTip: "radio",
        title: "选择属性集"
      })
      .then(async res => {
        if (!res.success) return;
        const classifierId = res.selectedId as string;
        const diagramModel = await modelService.getModelData(app.activeProject!.id, diagramId);

        await archService.addNewActualMeasurementInTable(app.activeProject!.id, classifierId, diagramModel.ownerId, diagramId);

        await tableEditor.refreshAll();
      });
  }
}
